package cn.xf.server.controller.api;

import cn.xf.server.bean.ResEnv;
import cn.xf.server.service.ConfigService;
import cn.xf.server.vo.ConfigExtVO;
import cn.xf.server.vo.XxlConfParamVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/conf")
public class ConfigServerController {

    @Autowired
    private ConfigService configService;

    @PostMapping("/find")
    public ResEnv<?> find(@RequestBody XxlConfParamVO jsonObject) {

         System.out.println(jsonObject);
        List<ConfigExtVO> configs = configService.listByProfile(jsonObject);

        Map<String, Object> res = new HashMap<>();
        configs.stream().filter(config -> config != null && config.getKey() != null).forEach(configExtVO -> res.put(configExtVO.getKey(), configExtVO.getValue()));
        return ResEnv.success(res);
    }
}
